<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\Di\Annotation\Inject;
use App\Utils\ApiUtils;
use App\Service\LotteryService;
use App\Validate\LotteryValidation;
use App\Constants\ErrorCode;

class LotteryController extends AbstractController
{

    
     /**
     * @Inject
     * @var LotteryService
     */
    protected $service;
    
    //获取抽奖次数列表
    public function list()
    {
        $pageSize = $this->request->input('pageSize', 10);
        //搜索条件
        $start_add_time =  $this->request->input('start_add_time', 0);
        $end_add_time =  $this->request->input('end_add_time', 0);
        $where = [];
        if ($start_add_time && $end_add_time) {
            $where = [['add_time', '>', strtotime(strval($start_add_time))], ['add_time', '<', strtotime(strval($end_add_time))]];
        } else if ($start_add_time) {
            $where = [['add_time', '>', strtotime(strval($start_add_time))]];
        } else if ($end_add_time) {
            $where = [['add_time', '<', strtotime(strval($end_add_time))]];
        }

         //搜索条件
         $type = (int)$this->request->input('type', 0);
         if($type){
             $where[] = ['type','=' ,$type];
         }
        $nickname = $this->request->input('nickname', '');
        $result=$this->service->list($nickname,$where,$pageSize);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $result['data'], $result['count']));

    }


    
    public function outputLog(){
         //搜索条件
         $start_add_time =  $this->request->input('start_add_time', 0);
         $end_add_time =  $this->request->input('end_add_time', 0);
         $where = [];
         if ($start_add_time && $end_add_time) {
             $where = [['add_time', '>', strtotime(strval($start_add_time))], ['add_time', '<', strtotime(strval($end_add_time))]];
         } else if ($start_add_time) {
             $where = [['add_time', '>', strtotime(strval($start_add_time))]];
         } else if ($end_add_time) {
             $where = [['add_time', '<', strtotime(strval($end_add_time))]];
         }
 
          //搜索条件
          $type = (int)$this->request->input('type', 0);
          if($type){
              $where[] = ['type','=' ,$type];
          }
         $nickname = $this->request->input('nickname', '');
         $filename=$this->service->outputLog(compact('nickname','where'));
        return $this->response->download($filename, '抽奖记录.csv');
    }

    /**
     * 系统设置-奖品列表
     * @Author czm
     * @DateTime 2020-04-17
     * @return void
     */
    public function prize()
    {
        $data=$this->service->prize();
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$data));

    }

    /**
     * 系统设置-奖品列表-奖品新增
     * @Author czm
     * @DateTime 2020-04-17
     * @return void
     */
    public function prizeAdd()
    {   
        make(LotteryValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $name=$this->request->input('name',"");
        $batch_id=intval($this->request->input('batch_id',0));
        $type=(int)$this->request->input('type',1);
        $probability=(int)$this->request->input('probability',0);
        $sort=(int)$this->request->input('sort',1);
        $status=(int)$this->request->input('status',1);
        $img=$this->request->input('img',"");
        $this->service->prizeAdd(compact('name','batch_id','type','probability','status','img','sort'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'新增成功'));

    }

    /**
     * 系统设置-奖品列表-奖品编辑
     * @Author czm
     * @DateTime 2020-04-17
     * @return void
     */
    public function prizeEdit()
    {
        make(LotteryValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $id=(int)$this->request->input('id',0);
        $name=$this->request->input('name',"");
        $batch_id=intval($this->request->input('batch_id',0));
        $type=(int)$this->request->input('type',1);
        $probability=(int)$this->request->input('probability',0);
        $status=(int)$this->request->input('status',1);
        $sort=(int)$this->request->input('sort',1);
        $img=$this->request->input('img',"");
        $this->service->prizeEdit(compact('id','name','batch_id','type','probability','status','img','sort'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'编辑成功'));

    }

    /**
     * 系统设置-奖品列表-奖品停用
     * @Author czm
     * @DateTime 2020-04-17
     * @return void
     */
    // public function prizeForbid()
    // {
    //     $id= (int)$this->request->input('id',0);
    //     if(empty($id)){
    //         return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS,'参数为空'));
    //     }
    //     $info=LotteryPrize::find($id);
    //     if(empty($info)){
    //         return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS,'奖品为空'));
    //     }
    //     $logInfo=$info->toJson(JSON_UNESCAPED_UNICODE);
    //     $info->status=2;
    //     $info->save();
    //     $this->adminLogLogic->addLog('停用了抽奖物品'.$logInfo);
    //     return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'已停用'));

    // }

    /**
     * 系统设置-奖品列表-奖品删除
     * @Author czm
     * @DateTime 2020-04-17
     * @return void
     */
    public function prizeDel()
    {
        make(LotteryValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $this->service->prizeDel($this->request->all());
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'已删除'));

    }
}
